//package org.convallaria.system.biz.controller.other;
//
//import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.tags.Tag;
//import lombok.RequiredArgsConstructor;
//import org.convallaria.framework.security.annotation.RequiresPermissions;
//import org.convallaria.system.biz.service.CacheOptimizationService;
//import org.convallaria.system.biz.service.PerformanceMonitorService;
//import org.convallaria.system.biz.service.auth.AuthService;
//import org.convallaria.system.biz.util.PerformanceTestUtils;
//import org.convallaria.system.biz.util.DocumentationGenerator;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * 性能监控控制器
// *
// * @author convallaria
// * @since 1.0.0
// */
//@Tag(name = "性能监控", description = "系统性能监控相关接口")
//@RestController
//@RequestMapping("/performance")
//@RequiredArgsConstructor
//public class PerformanceMonitorController {
//
//    private final PerformanceMonitorService performanceMonitorService;
//    private final CacheOptimizationService cacheOptimizationService;
//    private final AuthService authService;
//    private final PerformanceTestUtils performanceTestUtils;
//    private final DocumentationGenerator documentationGenerator;
//
//    @RequiresPermissions("system:performance:monitor")
//    @Operation(summary = "获取性能统计", description = "获取系统性能统计信息")
//    @GetMapping("/statistics")
//    public PerformanceMonitorService.PerformanceStatistics getPerformanceStatistics(
//            @RequestParam(defaultValue = "24") int timeRange) {
//        return performanceMonitorService.getPerformanceStatistics(timeRange);
//    }
//
//    @RequiresPermissions("system:performance:monitor")
//    @Operation(summary = "获取慢操作列表", description = "获取系统慢操作列表")
//    @GetMapping("/slow-operations")
//    public List<PerformanceMonitorService.SlowOperation> getSlowOperations(
//            @RequestParam(defaultValue = "10") int limit) {
//        return performanceMonitorService.getSlowOperations(limit);
//    }
//
//    @RequiresPermissions("system:performance:monitor")
//    @Operation(summary = "获取系统资源使用情况", description = "获取系统资源使用情况")
//    @GetMapping("/system-resources")
//    public PerformanceMonitorService.SystemResourceUsage getSystemResourceUsage() {
//        return performanceMonitorService.getSystemResourceUsage();
//    }
//
//    @RequiresPermissions("system:performance:monitor")
//    @Operation(summary = "获取性能概览", description = "获取系统性能概览信息")
//    @GetMapping("/overview")
//    public Map<String, Object> getPerformanceOverview() {
//        Map<String, Object> overview = new HashMap<>();
//
//        // 性能统计
//        PerformanceMonitorService.PerformanceStatistics stats =
//            performanceMonitorService.getPerformanceStatistics(24);
//        overview.put("performance", Map.of(
//            "totalRequests", stats.getTotalRequests(),
//            "successRate", stats.getSuccessRate(),
//            "averageResponseTime", stats.getAverageResponseTime(),
//            "p95ResponseTime", stats.getP95ResponseTime(),
//            "p99ResponseTime", stats.getP99ResponseTime(),
//            "slowOperations", stats.getSlowOperations()
//        ));
//
//        // 系统资源
//        PerformanceMonitorService.SystemResourceUsage resources =
//            performanceMonitorService.getSystemResourceUsage();
//        overview.put("resources", Map.of(
//            "cpuUsage", resources.getCpuUsage(),
//            "memoryUsagePercent", resources.getMemoryUsagePercent(),
//            "diskUsagePercent", resources.getDiskUsagePercent(),
//            "connectionUsagePercent", resources.getConnectionUsagePercent()
//        ));
//
//        // 缓存统计
//        CacheOptimizationService.CacheStatistics cacheStats =
//            cacheOptimizationService.getCacheStatistics();
//        overview.put("cache", Map.of(
//            "totalKeys", cacheStats.getTotalKeys(),
//            "hitRate", cacheStats.getHitRate(),
//            "memoryUsage", cacheStats.getMemoryUsage(),
//            "maxMemory", cacheStats.getMaxMemory()
//        ));
//
//        return overview;
//    }
//
//    @RequiresPermissions("system:performance:optimize")
//    @Operation(summary = "预热缓存", description = "预热系统缓存")
//    @PostMapping("/cache/warmup")
//    public Map<String, String> warmupCache() {
//        try {
//            cacheOptimizationService.warmupCache();
//            return Map.of("status", "success", "message", "缓存预热已启动");
//        } catch (Exception e) {
//            return Map.of("status", "error", "message", "缓存预热失败: " + e.getMessage());
//        }
//    }
//
//    @RequiresPermissions("system:performance:optimize")
//    @Operation(summary = "优化缓存配置", description = "优化缓存配置")
//    @PostMapping("/cache/optimize")
//    public Map<String, String> optimizeCache() {
//        try {
//            cacheOptimizationService.optimizeCacheConfig();
//            return Map.of("status", "success", "message", "缓存配置优化完成");
//        } catch (Exception e) {
//            return Map.of("status", "error", "message", "缓存配置优化失败: " + e.getMessage());
//        }
//    }
//
//    @RequiresPermissions("system:performance:monitor")
//    @Operation(summary = "获取缓存统计", description = "获取缓存统计信息")
//    @GetMapping("/cache/statistics")
//    public CacheOptimizationService.CacheStatistics getCacheStatistics() {
//        return cacheOptimizationService.getCacheStatistics();
//    }
//
//    @RequiresPermissions("system:performance:monitor")
//    @Operation(summary = "清理过期缓存", description = "清理过期的缓存数据")
//    @PostMapping("/cache/clean")
//    public Map<String, String> cleanExpiredCache() {
//        try {
//            cacheOptimizationService.cleanExpiredCache();
//            return Map.of("status", "success", "message", "过期缓存清理完成");
//        } catch (Exception e) {
//            return Map.of("status", "error", "message", "过期缓存清理失败: " + e.getMessage());
//        }
//    }
//
//    @RequiresPermissions("system:performance:monitor")
//    @Operation(summary = "获取性能健康检查", description = "获取系统性能健康检查结果")
//    @GetMapping("/health-check")
//    public Map<String, Object> getPerformanceHealthCheck() {
//        Map<String, Object> healthCheck = new HashMap<>();
//
//        // 性能指标检查
//        PerformanceMonitorService.PerformanceStatistics stats =
//            performanceMonitorService.getPerformanceStatistics(1);
//        boolean performanceHealthy = stats.getSuccessRate() > 0.95 &&
//                                   stats.getAverageResponseTime() < 500;
//
//        // 资源使用检查
//        PerformanceMonitorService.SystemResourceUsage resources =
//            performanceMonitorService.getSystemResourceUsage();
//        boolean resourceHealthy = resources.getCpuUsage() < 80 &&
//                                 resources.getMemoryUsagePercent() < 80;
//
//        // 缓存健康检查
//        CacheOptimizationService.CacheStatistics cacheStats =
//            cacheOptimizationService.getCacheStatistics();
//        boolean cacheHealthy = cacheStats.getHitRate() > 0.8;
//
//        // 整体健康状态
//        boolean overallHealthy = performanceHealthy && resourceHealthy && cacheHealthy;
//
//        healthCheck.put("overall", overallHealthy ? "HEALTHY" : "UNHEALTHY");
//        healthCheck.put("performance", performanceHealthy ? "HEALTHY" : "UNHEALTHY");
//        healthCheck.put("resources", resourceHealthy ? "HEALTHY" : "UNHEALTHY");
//        healthCheck.put("cache", cacheHealthy ? "HEALTHY" : "UNHEALTHY");
//
//        return healthCheck;
//    }
//
//    @RequiresPermissions("system:performance:test")
//    @Operation(summary = "执行并发登录测试", description = "执行并发登录性能测试")
//    @PostMapping("/test/concurrent-login")
//    public PerformanceTestUtils.LoadTestResult performConcurrentLoginTest(
//            @RequestParam(defaultValue = "10") int threadCount,
//            @RequestParam(defaultValue = "100") int requestCount) {
//        return performanceTestUtils.performConcurrentLoginTest(authService, threadCount, requestCount);
//    }
//
//    @RequiresPermissions("system:performance:test")
//    @Operation(summary = "执行缓存性能测试", description = "执行缓存性能测试")
//    @PostMapping("/test/cache")
//    public PerformanceTestUtils.CacheTestResult performCacheTest() {
//        return performanceTestUtils.performCacheTest(cacheOptimizationService);
//    }
//
//    @RequiresPermissions("system:performance:test")
//    @Operation(summary = "执行性能监控测试", description = "执行性能监控测试")
//    @PostMapping("/test/performance")
//    public PerformanceTestUtils.PerformanceTestResult performPerformanceTest() {
//        return performanceTestUtils.performPerformanceTest(performanceMonitorService);
//    }
//
//    @RequiresPermissions("system:documentation:generate")
//    @Operation(summary = "生成系统配置文档", description = "生成系统配置文档")
//    @GetMapping("/documentation/configuration")
//    public Map<String, String> generateConfigurationDocument() {
//        try {
//            String document = documentationGenerator.generateConfigurationDocument(null);
//            return Map.of("status", "success", "document", document);
//        } catch (Exception e) {
//            return Map.of("status", "error", "message", "生成配置文档失败: " + e.getMessage());
//        }
//    }
//
//    @RequiresPermissions("system:documentation:generate")
//    @Operation(summary = "生成性能监控文档", description = "生成性能监控文档")
//    @GetMapping("/documentation/performance")
//    public Map<String, String> generatePerformanceDocument() {
//        try {
//            String document = documentationGenerator.generatePerformanceDocument(performanceMonitorService);
//            return Map.of("status", "success", "document", document);
//        } catch (Exception e) {
//            return Map.of("status", "error", "message", "生成性能文档失败: " + e.getMessage());
//        }
//    }
//
//    @RequiresPermissions("system:documentation:generate")
//    @Operation(summary = "生成缓存优化文档", description = "生成缓存优化文档")
//    @GetMapping("/documentation/cache")
//    public Map<String, String> generateCacheDocument() {
//        try {
//            String document = documentationGenerator.generateCacheDocument(cacheOptimizationService);
//            return Map.of("status", "success", "document", document);
//        } catch (Exception e) {
//            return Map.of("status", "error", "message", "生成缓存文档失败: " + e.getMessage());
//        }
//    }
//
//    @RequiresPermissions("system:documentation:generate")
//    @Operation(summary = "生成API接口文档", description = "生成API接口文档")
//    @GetMapping("/documentation/api")
//    public Map<String, String> generateApiDocument() {
//        try {
//            String document = documentationGenerator.generateApiDocument();
//            return Map.of("status", "success", "document", document);
//        } catch (Exception e) {
//            return Map.of("status", "error", "message", "生成API文档失败: " + e.getMessage());
//        }
//    }
//
//    @RequiresPermissions("system:documentation:generate")
//    @Operation(summary = "生成完整系统文档", description = "生成完整系统文档")
//    @GetMapping("/documentation/complete")
//    public Map<String, String> generateCompleteDocument() {
//        try {
//            String document = documentationGenerator.generateCompleteDocument(null, performanceMonitorService, cacheOptimizationService);
//            return Map.of("status", "success", "document", document);
//        } catch (Exception e) {
//            return Map.of("status", "error", "message", "生成完整文档失败: " + e.getMessage());
//        }
//    }
//}
